package ma.ensao.evalmetrics.interpretation;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import ma.ensao.evalmetrics.controller.CaracteristiquesManager;
import ma.ensao.evalmetrics.controller.MetriquesManager;
import ma.ensao.evalmetrics.controller.ProprieteManager;
import ma.ensao.evalmetrics.controller.ResultManager;
import ma.ensao.evalmetrics.controller.SubcaracManager;
import ma.ensao.evalmetrics.controller.metrics.Calcule;
import ma.ensao.evalmetrics.model.CaracInt;
import ma.ensao.evalmetrics.model.Caracteristiques;
import ma.ensao.evalmetrics.model.Metriques;
import ma.ensao.evalmetrics.model.Propriete;
import ma.ensao.evalmetrics.model.ProprieteInt;
import ma.ensao.evalmetrics.model.Result;
import ma.ensao.evalmetrics.model.Subcarac;
import ma.ensao.evalmetrics.model.SubcaracInt;

import com.opensymphony.xwork2.ActionSupport;

public class CaracIntManager extends ActionSupport {
	

	private static final long serialVersionUID = 1L;
	private Long idp;
	private ResultManager resultmanager = new ResultManager();
	private List<Result> resultlist;
	private MetriquesManager metriquesmanager = new MetriquesManager();
	private ProprieteManager proprietemanager = new ProprieteManager();
	private CaracteristiquesManager caracteristiques = new CaracteristiquesManager();
	private SubcaracManager subcarac = new SubcaracManager();
	private Calcule calcule = new Calcule();
	private Propriete propriete,propres;
	private Metriques metric,metricres;
	private Subcarac sub,subres;
	private Caracteristiques carac,caracres;
	private List<CaracInt> records;
	private String result;
	private int TotalRecordCount,jtStartIndex,jtPageSize;
	String jtSorting;
	
	//
	private Long id;
	
	public String afficher() {

		records = new ArrayList<CaracInt>() ;
		resultlist=resultmanager.listAll(idp,jtStartIndex,jtPageSize,jtSorting);
		metric = metriquesmanager.list(idp).get(0);
		propriete = proprietemanager.list(idp).get(0);
		sub = subcarac.list(idp).get(0);
		carac = caracteristiques.list(idp).get(0);
		CaracInt caracint;
		ProprieteInt proprieteInt;
		SubcaracInt subint;
		for(Result res : resultlist){
			metricres = calcule.calculMetrique(metric, res);
			propres = calcule.calculPropriete(propriete);
			propres.setId(res.getId());
			subres = calcule.calculSubcarac(sub);
			
			proprieteInt = ProprieteIntManager.map(propres,metricres);
			subint = SubcaracIntManager.map(subres,proprieteInt);
			caracres = calcule.calculCaracteristiques(carac);
			caracint = map(caracres,subint);
			caracint.setDate(res.getDate());
			caracint.setId(res.getId());
			records.add(caracint);
		}
		result="OK";
		TotalRecordCount = resultmanager.countTotal(idp);

		return SUCCESS;
	}
	
	public String delete() {
		
		Result res = new Result();

		res.setId(id);
		System.out.println("id"+id);
		
		resultmanager.deleteResult(res);
		
		result="OK";
		
		return SUCCESS;
	}
	
	public static CaracInt map(Caracteristiques oldcarac,SubcaracInt sub){
		CaracInt carac = new CaracInt();
		
		carac.setFonctionnalite(oldcarac.getFonctionnalite());
		carac.setReliabilite(oldcarac.getReliabilite());
		carac.setUsabilite(oldcarac.getUsabilite());
		carac.setEfficacite(oldcarac.getEfficacite());
		carac.setMaintenabilite(oldcarac.getMaintenabilite());
		carac.setPortabilite(oldcarac.getPortabilite());
		
		//Fonctionnalite
		double interp = (sub.getInteroperabiliteInt()+sub.getSecuriteInt())/2;
		interp = new BigDecimal(interp ).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
		carac.setFonctionnaliteInt(interp);
		//Reliabilite
		interp = (sub.getMaturiteInt()+sub.getRecoverabiliteInt()+sub.getFausseToleranceInt())/3;
		interp = new BigDecimal(interp ).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
		carac.setReliabiliteInt(interp);
		//Usabilite
		interp = sub.getAttractiviteInt();
		interp = new BigDecimal(interp ).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
		carac.setUsabiliteInt(interp);
		//Efficacite : no prop or subcarac to calculate from .
		//Maintenabilite
		interp = (sub.getVariabiliteInt()+sub.getAnalyseInt()+sub.getStabiliteInt()+sub.getTestabiliteInt())/4;
		interp = new BigDecimal(interp ).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
		carac.setMaintenabiliteInt(interp);
		//Portabilite
		interp = (sub.getAdabtabiliteInt()+sub.getInstabiliteInt()+sub.getConformiteInt())/3;
		interp = new BigDecimal(interp ).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
		carac.setPortabiliteInt(interp);
		
		return carac;
	}

	public Long getIdp() {
		return idp;
	}

	public void setIdp(Long idp) {
		this.idp = idp;
	}

	public List<CaracInt> getRecords() {
		return records;
	}

	public void setRecords(List<CaracInt> records) {
		this.records = records;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public int getTotalRecordCount() {
		return TotalRecordCount;
	}

	public void setTotalRecordCount(int totalRecordCount) {
		TotalRecordCount = totalRecordCount;
	}

	public int getJtStartIndex() {
		return jtStartIndex;
	}

	public void setJtStartIndex(int jtStartIndex) {
		this.jtStartIndex = jtStartIndex;
	}

	public int getJtPageSize() {
		return jtPageSize;
	}

	public void setJtPageSize(int jtPageSize) {
		this.jtPageSize = jtPageSize;
	}

	public String getJtSorting() {
		return jtSorting;
	}

	public void setJtSorting(String jtSorting) {
		this.jtSorting = jtSorting;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	
	
}
